﻿Imports Microsoft.Office.Interop.Outlook

Namespace UI
    Public Class pmFoldertree
        Private _app As Outlook.Application
        Private _mailItem As MailItem
        Private _searchPattern As String

        Public WriteOnly Property SearchPattern() As String
            Set(ByVal value As String)
                _searchPattern = value
            End Set
        End Property

        Friend Sub New(ByVal mailItem As MailItem, ByVal App As Outlook.Application)
            If IsNothing(App) Then
                Throw New ArgumentNullException("App")
            End If
            'If IsNothing(mailItem) Then
            '    Throw New ArgumentNullException("mailItem")
            'End If

            _mailItem = mailItem
            _app = App

            ' This call is required by the Windows Form Designer.
            InitializeComponent()
        End Sub

        Friend Sub New(ByVal App As Outlook.Application)
            If IsNothing(App) Then
                Throw New ArgumentNullException("App")
            End If

            _app = App

            ' This call is required by the Windows Form Designer.
            InitializeComponent()
        End Sub

        Private Sub pmFoldertree_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DisplayFolderTree()
        End Sub

#Region "tree"
        Private Sub DisplayFolderTree()
            Dim inbox As Outlook.MAPIFolder = SharedFunctions.GetInboxFolder(_app)
            Dim root As TreeNode = CreateTreeNode(inbox.Name, inbox, inbox.FolderPath)

            TreeView1.Nodes.Add(root)
            AddMailFoldersToTreeview(root, inbox)
            root.ExpandAll()
            TreeView1.DrawMode = TreeViewDrawMode.OwnerDrawText
        End Sub

        Private Sub AddMailFoldersToTreeview(ByVal parentNode As TreeNode, ByVal rootFolder As Outlook.MAPIFolder)
            Dim sorted = From n As MAPIFolder In rootFolder.Folders.OfType(Of MAPIFolder)() _
                    Order By n.Name _
                    Select n

            For Each folder As Outlook.MAPIFolder In sorted
                If folder.DefaultItemType = Outlook.OlItemType.olMailItem Then
                    Dim childNode As TreeNode = CreateTreeNode(folder.Name, folder, folder.FolderPath)

                    parentNode.Nodes.Add(childNode)
                    AddMailFoldersToTreeview(childNode, folder)
                End If
            Next
        End Sub

        Private Function CreateTreeNode(ByVal text As String, ByVal tag As MAPIFolder, ByVal name As String) As TreeNode
            Dim node As New TreeNode(text)
            node.Tag = tag
            node.Name = name

            Return node
        End Function

        Public Sub SelectRelevantFolder()
            SelectFolderInTree(GetFolderBySearchPattern(SharedFunctions.GetInboxFolder(_app)))
        End Sub

        Public Sub SelectFolderInTree(ByVal folderToSelect As MAPIFolder)
            If folderToSelect Is Nothing Then
                TreeView1.SelectedNode = TreeView1.Nodes(0)
                Return
            End If
            SelectFolderInTree(TreeView1.Nodes(0), folderToSelect)

            If TreeView1.SelectedNode.Text = folderToSelect.Name Then 'het kan zijn dat de folder al is geselecteerd
                Return
            End If

            For Each node As TreeNode In TreeView1.Nodes(0).Nodes
                If TryCast(node.Tag, MAPIFolder).FolderPath.Equals(folderToSelect.FolderPath) Then
                    TreeView1.SelectedNode = node
                    Return
                End If
            Next
            TreeView1.SelectedNode = TreeView1.Nodes(0)
        End Sub

        Private Sub SelectFolderInTree(ByVal parentNode As TreeNode, ByVal folderToSelect As MAPIFolder)
            For Each node As TreeNode In parentNode.Nodes
                If TryCast(node.Tag, MAPIFolder).FolderPath.Equals(folderToSelect.FolderPath) Then
                    TreeView1.SelectedNode = node
                    Return
                End If
                SelectFolderInTree(node, folderToSelect)
            Next
        End Sub

        Public Function GetSelectedFolder() As MAPIFolder
            If IsNothing(TreeView1.SelectedNode) Then
                Throw New InvalidOperationException("Gelieve een folder te selecteren")
            End If
            Return DirectCast(TreeView1.SelectedNode.Tag, MAPIFolder)
        End Function

        Private Sub TreeView1_DrawNode(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawTreeNodeEventArgs) Handles TreeView1.DrawNode
            Dim f As Font = If(e.Node.NodeFont IsNot Nothing, e.Node.NodeFont, e.Node.TreeView.Font)

            Dim sz As Size = TextRenderer.MeasureText(e.Node.Text, f)
            Dim rc As New Rectangle(e.Bounds.X - 1, e.Bounds.Y, sz.Width + 2, e.Bounds.Height)
            Dim fore As Color = e.Node.ForeColor

            If fore = Color.Empty Then
                fore = e.Node.TreeView.ForeColor
            End If

            If e.Node Is e.Node.TreeView.SelectedNode Then
                fore = SystemColors.HighlightText
                'If (e.State And TreeNodeStates.Focused) <> 0 Then
                '    fore = Color.Black
                'End If
            End If

            Dim back As Color = e.Node.BackColor
            If back = Color.Empty Then
                back = e.Node.TreeView.BackColor
            End If
            If e.Node Is e.Node.TreeView.SelectedNode Then
                back = SystemColors.Highlight
            End If
            Dim bbr As New SolidBrush(back)
            e.Graphics.FillRectangle(bbr, rc)

            TextRenderer.DrawText(e.Graphics, e.Node.Text, f, rc, fore, back)
            bbr.Dispose()
        End Sub
#End Region

#Region "Private methods"
        Private Function GetFolderBySearchPattern(ByVal rootFolder As MAPIFolder) As MAPIFolder
            Dim returnedFolder As MAPIFolder = Nothing
            If _searchPattern <> String.Empty Then
                For Each folder As MAPIFolder In rootFolder.Folders
                    If folder.Name.IndexOf(_searchPattern, StringComparison.CurrentCultureIgnoreCase) <> -1 Then
                        Return (folder)
                    End If
                    returnedFolder = GetFolderBySearchPattern(folder)
                    If returnedFolder IsNot Nothing Then
                        Return returnedFolder
                    End If
                Next
            End If
            Return Nothing
        End Function
#End Region

        Public Function GetFolderByName(ByVal rootFolder As MAPIFolder, ByVal name As String) As MAPIFolder
            Dim returnedFolder As MAPIFolder = Nothing
            For Each folder As MAPIFolder In rootFolder.Folders
                If folder.Name.ToLower.Equals(name.ToLower) Then
                    Return (folder)
                End If
            Next
            Return Nothing
        End Function
    End Class
End Namespace
